package org.aplombh.java.utils.multiThread.create;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import static org.aplombh.java.utils.WebDownloader.downloader;

/**
 * 继承Thread类
 * <p>
 * 子类继承Thread类具备多线程能力
 * 启动线程:子类对象. start()
 * 不建议使用: 避免OOP单继承局限性
 */
@AllArgsConstructor
@NoArgsConstructor
public class TestCallable implements Callable<Boolean> {
    private String url;
    private String name;

    @Override
    public Boolean call() {
        downloader(url, name);
        System.out.println("download:" + name);
        return true;
    }


    public static void main(String[] args) throws Exception {
        TestCallable callable1 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "1.jpg");
        TestCallable callable2 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "2.jpg");
        TestCallable callable3 = new TestCallable("https://www.baidu.com/img/flexible/logo/pc/result.png", "3.jpg");

        FutureTask<Boolean> result1 = new FutureTask<>(callable1);
        FutureTask<Boolean> result2 = new FutureTask<>(callable2);
        FutureTask<Boolean> result3 = new FutureTask<>(callable3);

        new Thread(result1).start();
        new Thread(result2).start();
        new Thread(result3).start();

        Boolean resultBoolean1 = result1.get();
        Boolean resultBoolean2 = result2.get();
        Boolean resultBoolean3 = result3.get();

        System.out.println(resultBoolean1);
        System.out.println(resultBoolean2);
        System.out.println(resultBoolean3);

    }
}
